package com.example.broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private MyReceiver myReceiver;
    private Button btn1,btn2,btn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
        btn3=findViewById(R.id.btn3);

        btn1.setOnClickListener(view -> {
            myReceiver =new MyReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction("WEATHER_REPORT");
            filter.addAction("NEWS_REPORT");
            registerReceiver(myReceiver,filter);
        });

        btn2.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setAction("WEATHER_REPORT");
            intent.putExtra("CITY","清城");
            intent.putExtra("WEATHER","多云");
            intent.putExtra("HIGH",30);
            sendBroadcast(intent);
        });

        btn3.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setAction("NEWS_REPORT");
            intent.putExtra("TITLE","1000总决赛");
            intent.putExtra("TIME","2023年11月8日");
            intent.putExtra("ADDR","清远校区");
            sendBroadcast(intent);
        });

    }
}